**********************************************************************************************
* Replication file: "Low skill products by high skill workers. The distributive effects of trade in emerging and developing countries"
* Irene Menéndez González, Erica Owen and Stefanie Walter. December 2022
**********************************************************************************************

**********************************************************************************************
* Supplementary materials for macro-level analysis
**********************************************************************************************

clear
set more off

capture log close

use DataIneqTrade_ready.dta, clear


* DV: market and disposable income inequality
***********************************************************************
* mgini_mk: measure of market income inequality containing the row mean of the inequality estimates
 
* mgini_dp: measure of disposable income inequality containing the row mean of the inequality estimates


* Main IVs
***********************************************************************

* Trade volume: imports + exports as % GDP
gen tradef = trade/100

* Exports in manufacturing (% merchandise)
gen manufexpf = manufexp/100

* Imports in manufacturing (% merchandise)
gen manufimpf = manufimp/100

* Exports in agriculture (% of merchandise) 
gen agrexpf = agrexp/100

* Imports in agriculture (% of merchandise)
gen agrimpf = agrimp/100

* Exports in food (% of merchandise) 
gen foodexpf = foodexp/100

* Imports in food (% of merchandise)
gen foodimpf = foodimp/100

* Exports in fuels (% of merchandise) 
gen fuelexpf=fuelexp/100

* Imports in fuels (% of merchandise) 
gen fuelimpf=fuelimp/100

* Sum of commodities
gen comtotalx=agrexpf + foodexpf + fuelexpf
gen comtotalm=agrimpf + foodimpf + fuelimpf

* Polity indicator
gen demo=.
replace demo=1 if polity>=6
replace demo=0 if polity<6
lab def de 0 "Non-democracy" 1 "Democracy"
lab val demo de
lab var demo "Democracy"

* Ideology
gen ideo=.
replace ideo=1 if execrlc==3
replace ideo=0 if execrlc==2
replace ideo=0 if execrlc==1

lab def id 0 "Center/Right" 1 "Left"
lab val ideo id
lab var ideo "Ideology"

* Proportional representation - pr (3 categories) 
recode pr (1=2) (0=1) (-999=0)
lab def r 0 "NA" 1 "Plurality" 2 "PR"
lab val pr r
lab var pr "Proportional representation"

* Capita GDP (log) 
gen log_gdp=log(gdpcap)

* Capita GDP squared
gen gdpsq=log_gdp^2

* Population growth (log)
gen log_pop=log(pop)

* Growth gdp (log): annual % growth rate GDP
gen log_grthgdp=log(grthgdp)

xtset acode year 

* Lags

gen manufexpfL1=L1.manufexpf

gen manufimpfL1=L1.manufimpf

gen foodexpfL1=L1.foodexpf

gen foodimpfL1=L1.foodimpf

gen agrexpfL1=L1.agrexpf

gen agrimpfL1=L1.agrimpf

gen fuelexpfL1=L1.fuelexpf

gen fuelimpfL1=L1.fuelimpf

gen comtotalxL1=L1.comtotalx

gen comtotalmL1=L1.comtotalm

gen popL1=L1.pop

gen log_gdpL1=L1.log_gdp

gen gdpsqL1=L1.gdpsq

gen tradefL1=L1.tradef

gen grthgdpL1=L1.grthgdp

gen prL1=L1.pr

gen ideoL1=L1.ideo

gen demoL1=L1.demo

* Period dummies
gen period=.

replace period=0 if year==1960
replace period=0 if year==1961
replace period=0 if year==1962
replace period=0 if year==1963
replace period=0 if year==1964
replace period=0 if year==1965
replace period=0 if year==1966
replace period=0 if year==1967
replace period=0 if year==1968
replace period=0 if year==1969
replace period=1 if year==1970
replace period=1 if year==1971
replace period=1 if year==1972
replace period=1 if year==1973
replace period=1 if year==1974
replace period=1 if year==1975
replace period=1 if year==1976
replace period=1 if year==1977
replace period=1 if year==1978
replace period=1 if year==1979
replace period=2 if year==1980
replace period=2 if year==1981
replace period=2 if year==1982
replace period=2 if year==1983
replace period=2 if year==1984
replace period=2 if year==1985
replace period=2 if year==1986
replace period=2 if year==1987
replace period=2 if year==1988
replace period=2 if year==1989
replace period=3 if year==1990
replace period=3 if year==1991
replace period=3 if year==1992
replace period=3 if year==1993
replace period=3 if year==1994
replace period=3 if year==1995
replace period=3 if year==1996
replace period=3 if year==1997
replace period=3 if year==1998
replace period=3 if year==1999
replace period=4 if year==2000
replace period=4 if year==2001
replace period=4 if year==2002
replace period=4 if year==2003
replace period=4 if year==2004
replace period=4 if year==2005
replace period=4 if year==2006
replace period=4 if year==2007
replace period=4 if year==2008
replace period=4 if year==2009
replace period=5 if year==2010
replace period=5 if year==2011
replace period=5 if year==2012
replace period=5 if year==2013
replace period=5 if year==2014
replace period=5 if year==2015
replace period=5 if year==2016

xtset acode year 



* Descriptive statistics
***********************************************************************

asdoc sum mgini_mk mgini_dp manufexpf manufimpf tradef foodexpf foodimpf agrexpf agrimpf fuelexpf fuelimpf comtotalx comtotalm demo log_gdp gdpsq grthgdp pop ideo pr if region==1, label replace


* Regression models
***********************************************************************

* Table A13. Effect of trade exposure on market and disposable income inequality in emerging markets and developing countries, controlling for food, agricultural and mineral fuels exports/imports, 1960-2016

* Market income inequality
* Model 1. With food exports/imports
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 foodexpfL1 foodimpfL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro2.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 2. With agricultural exports/imports
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 agrexpfL1 agrimpfL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro2.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 3. With fuel exports/imports
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 fuelexpfL1 fuelimpfL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro2.doc, append ctitle(Model 3) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Disposable income inequality
* Model 4. With food exports/imports
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 foodexpfL1 foodimpfL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro2.doc, append ctitle(Model 4) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 5. With agricultural exports/imports
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 agrexpfL1 agrimpfL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro2.doc, append ctitle(Model 5) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 6. With fuel exports/imports
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 fuelexpfL1 fuelimpfL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro2.doc, append ctitle(Model 6) dec(4) label addtext(Country FE, Yes, Period FE, Yes)


* Table A14. Effect of trade exposure on market and disposable income inequality in emerging markets and developing countries, controlling for (log) capita GDP squared.

* Market income inequality
* Model 1. Controlling for trade volume
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 gdpsqL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro_sq.doc, append ctitle(Model 1) dec(4) label addtext(Country FE, Yes)

* Model 2. With period effects
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 gdpsqL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro_sq.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Disposable income inequality
* Model 3: Controlling for trade volume
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 gdpsqL1 grthgdpL1 popL1 if region==1, fe cluster(acode)
outreg2 using ldcmacro_sq.doc, append ctitle(Model 3) dec(4) label addtext(Country FE, Yes)

* Model 4: With period effects
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 gdpsqL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro_sq.doc, append ctitle(Model 4) dec(4) label addtext(Country FE, Yes, Period FE, Yes)


* Table A15. Effect of trade exposure on market and disposable income inequality in emerging markets and developing countries, excluding China.

* Market income inequality
* Model 1. Controlling for trade volume
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1 & acode!=35, fe cluster(acode) 
outreg2 using ldcmacro_ch.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes)

* Model 2. With period effects
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1 & acode!=35, fe cluster(acode) 
outreg2 using ldcmacro_ch.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Disposable income inequality
* Model 3. Controlling for trade volume
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1 & acode!=35, fe cluster(acode)
outreg2 using ldcmacro_ch.doc, append ctitle(Model 3) dec(4) label addtext(Country FE, Yes)

* Model 4. With period effects
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1 & acode!=35, fe cluster(acode) 
outreg2 using ldcmacro_ch.doc, append ctitle(Model 4) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 5. Based on model 4, restricting sample to countries used with market income inequality
* Brazil, Colombia, Dom Republic, Egypt, Georgia, Guatemala, India, Mexico, Panama, Paraguay, Peru, Romania, Serbia, S Africa.

xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1 & acode==23 | acode==36 | acode==49 | acode==51 | acode==61 | acode==66 | acode==75 | acode==109 | acode==129 | acode==131 | acode== 132 | acode==138 | acode==147 | acode==155, fe cluster(acode)  
outreg2 using ldcmacro_ch.doc, append ctitle(Model 5) dec(4) label addtext(Country FE, Yes, Period FE, Yes)


* Table A16. Effect of trade exposure on market and disposable income inequality in emerging markets and developing countries, controlling for country-specific time-trends.

* Model 1. Market income inequality
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 c.year#i.acode i.period if region==1, fe cluster(acode) 
outreg2 using cntimetrend.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes, Period FE, Yes, Country-specific time (year) trend, Yes)

* Model 2. Disposable income inequality
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 c.year#i.acode i.period if region==1, fe cluster(acode) 
outreg2 using cntimetrend.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes, Period FE, Yes, Country-specific time (year) trend, Yes)


* Tables A18 and A19: Sensitivity analyses

* We conduct sensitivity analysis based on Model 3 in Tables 3 and 4 (including country FE and controlling for trade volume, as well as our set of control variables). Both sets of analyses benchmark the strength of confounding with the growth GDP variable.

ssc install sensemakr

* Table A18
sensemakr mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.acode if region==1, treat(manufexpfL1) benchmark(grthgdpL1)

* Table A19
sensemakr mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.acode if region==1, treat(manufexpfL1) benchmark(grthgdpL1)



* Table A20: Dynamic panel system-GMM estimator

* Model 1. Without collapsing or limiting lag depth: 509 instruments, 15 groups.
xtabond2 mgini_mk L.mgini_mk manufexpf manufimpf i.period if region==1, gmm(L.mgini_mk manufexpf manufimpf) iv(manufexpf manufimpf i.period, eq(level)) orthogonal twostep robust nodiffsargan
outreg2 using gmm_mk.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes, Period FE, Yes) 

* Model 2. Limiting lag depth brings # instruments down to 394 from 509
xtabond2 mgini_mk L.mgini_mk manufexpf manufimpf i.period if region==1, gmm(L.mgini_mk manufexpf manufimpf, lag(1 2)) iv(manufexpf manufimpf i.period, eq(level)) orthogonal twostep robust nodiffsargan
outreg2 using gmm_mk.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 3. Collapsing brings # instruments down to 161 from 509
xtabond2 mgini_mk L.mgini_mk manufexpf manufimpf i.period if region==1, gmm(L.mgini_mk manufexpf manufimpf, collapse) iv(manufexpf manufimpf i.period, eq(level)) orthogonal twostep robust nodiffsargan
outreg2 using gmm_mk.doc, append ctitle(Model 3) dec(4) label addtext(Country FE, Yes, Period FE, Yes) 

* Model 4. Combining collapse and limited lag brings # instruments down to 17.
xtabond2 mgini_mk L.mgini_mk manufexpf manufimpf i.period if region==1, gmm(L.mgini_mk manufexpf manufimpf, lag(1 2) collapse) iv(manufexpf manufimpf i.period, eq(level)) orthogonal twostep robust nodiffsargan
outreg2 using gmm_mk.doc, append ctitle(Model 4) dec(4) label addtext(Country FE, Yes, Period FE, Yes) 


